<?php

namespace app\common\model;

use think\Model;

/**
 * 短信验证码
 */
class FishingGroundOrdersGroups extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    protected $insert = ['status' => 0];
    // 追加属性
    protected $append = [
    ];

    public function getCreateTimeAttr($value)
    {
        return is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value;
    }

    public function getUpdateTimeAttr($value)
    {
        return is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value;
    }

    public function positions()
    {
        return $this->belongsTo(FishingGroundPositions::class, 'id', 'position_id');
    }

    /**
     * 拼团成功
     * @param $orderId
     */
    public static function groupSucess($orderId)
    {
        self::where('order_id', $orderId)->where('status', 1)->update(['status' => 2]);
    }

    /**
     * 拼团失败
     * @param $orderId
     */
    public function groupFail($orderId)
    {
        self::where('order_id', $orderId)->where('status', 1)->update(['status' => 3]);
        $lists = self::where('order_id', $orderId)->where('status', 1)->select();
        foreach ($lists as $val) {
            // 退款
            switch ($val->pay_type) {
                case 1:
                    // 支付宝退款
                    break;
                case 2:
                    // 微信退款
                    break;
                case 3:
                    // 余额退款
                    break;
            }

            // 释放钓位
            FishingGroundPositions::where('id', $val->position_id)->update(['status' => 0]);
        }
    }

    /**
     * 取消订单
     * @param $orderId
     * @param $userId
     */
    public function cancleOrder($orderId, $userId)
    {
        $row_info = self::where('order_id', $orderId)->where('user_id', $userId)->where('status', 'in', [1, 2])->find();
        $row_info->status = 3;
        $row_info->save();
        // 退款
        switch ($row_info->pay_type) {
            case 1:
                // 支付宝退款
                break;
            case 2:
                // 微信退款
                break;
            case 3:
                // 余额退款
                break;
        }

        // 释放钓位
        FishingGroundPositions::where('id', $row_info->position_id)->update(['status' => 0]);
    }
}
